<div class="layuimini-container layuimini-page-anim">
  <div class="layuimini-main">
    <div class="demoTable">
      搜索用户：
      <div class="layui-inline">
        <input placeholder="用户名" class="layui-input" name="id" id="demoReload" autocomplete="off">
      </div>
      <button class="layui-btn" data-type="reload">搜索</button>
    </div>
    <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

    <script type="text/html" id="currentTableBar">
      <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
      <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
    </script>

  </div>
</div>

<script>
  layui.use(['form', 'table','miniPage','element'], function () {
    var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            miniPage = layui.miniPage;

    table.render({
      elem: '#currentTableId',
      url: '../user/page',
      defaultToolbar: ['filter', 'exports', 'print', {
        title: '提示',
        layEvent: 'LAYTABLE_TIPS',
        icon: 'layui-icon-tips'
      }],
      parseData: function (res) {
        let data = res.data.records;
        return {
          "code": 0,
          "msg": "错误",
          "count": res.data.total,
          "data": data
        }
      },
      request: {
        pageName: 'current' //页码的参数名称，默认：page
        ,limitName: 'size' //每页数据量的参数名，默认：limit
      }
      ,cols: [[
        {type: "checkbox", width: 50},
        {field: 'id', width: 80, title: 'ID', sort: true},
        {field: 'phone', title: '手机'},
        {field: 'name', title: '用户名'},
        {field: 'address', title: '地址'},
        {field: 'sex', title: '性别'},
        {field: 'email', title: '邮箱'},
        {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
      ]],
      limits: [10, 15, 20, 25, 50, 100],
      limit: 15,
      page: true,
      skin: 'line',
      id: 'testReload'
    });
    var $ = layui.$, active = {
      reload: function(){
        var demoReload = $('#demoReload');

        //执行重载
        table.reload('testReload', {
          page: {
            curr: 1 //重新从第 1 页开始
          }
          ,where: {
            name: demoReload.val()
          }
        });
      }
    };

    $('.demoTable .layui-btn').on('click', function(){
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });
    /**
     * toolbar事件监听
     */
    table.on('toolbar(currentTableFilter)', function (obj) {
      if (obj.event === 'add') {   // 监听添加操作
        var content = miniPage.getHrefContent('page/table/add.html');
        var openWH = miniPage.getOpenWidthHeight();

        var index = layer.open({
          title: '添加套餐',
          type: 1,
          shade: 0.2,
          maxmin:true,
          shadeClose: true,
          area: [openWH[0] + 'px', openWH[1] + 'px'],
          offset: [openWH[2] + 'px', openWH[3] + 'px'],
          content: content,
        });
        $(window).on("resize", function () {
          layer.full(index);
        });
      } else if (obj.event === 'delete') {  // 监听删除操作
        var checkStatus = table.checkStatus('currentTableId')
                , data = checkStatus.data;
        layer.alert(JSON.stringify(data));
      }
    });

    //监听表格复选框选择
    table.on('checkbox(currentTableFilter)', function (obj) {
      console.log(obj)
    });

    table.on('tool(currentTableFilter)', function (obj) {
      var data = obj.data;
      if (obj.event === 'edit') {
        console.log(data)
        localStorage.setItem('user1', JSON.stringify(data))
        var content = miniPage.getHrefContent('page/table/edit-user.html');
        var openWH = miniPage.getOpenWidthHeight();

        var index = layer.open({
          title: '编辑用户',
          type: 1,
          shade: 0.2,
          maxmin:true,
          shadeClose: true,
          area: [openWH[0] + 'px', openWH[1] + 'px'],
          offset: [openWH[2] + 'px', openWH[3] + 'px'],
          content: content,
        });
        $(window).on("resize", function () {
          layer.full(index);
        });
        return false;
      } else if (obj.event === 'delete') {
        layer.confirm('真的删除么', function (index) {
          $.ajax({
            url: '../user/delete/'+ obj.data.id,
            method: 'DELETE',
            dataType: 'json',
            success: function (resp) {
              obj.del();
              layer.close(index);
            }
          })
        });
      }
    });

  });
</script>